Ext.namespace("AuditPlan");

AuditPlan.Audited = function() {
	this.winReport = null;
	this.winViewPlan = null;
		this.dtStart = new Ext.form.DateField({
		fieldLabel : '开始时间',
		name : 'startDateFm',
		width : 85,
		format :'Y-m-d'
	});
	this.dtEnd=new Ext.form.DateField({
		fieldLabel:'～',
		name:'startDateTo',
		width: 85,
		format:'Y-m-d'
	});
	this.userStore=new Ext.data.JsonStore({
	    autoDestroy: true,
	    root:'users',
	    url: '../auditplan/selectCreators.form?timesnap=' + Date.parse(new Date()),
	    fields: ['userId', 'userName' ]
	});
	this.userStore.load();
	this.cbUser = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '创建人',
		name:'createId',
		store:this.userStore,
        valueField: 'userId',
        displayField: 'userName',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	
	this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找计划'},
        handler: this.queryPlan,
        scope: this
    });
	this.store = new Ext.data.JsonStore({
	    url: Utils.formatURL('../auditplan/auditedPlan.form?timesnap=' + Date.parse(new Date())),
	    root: 'result',
	    fields: ['planId', 'planName','planType','createId','createName','deptId','deptName','audotorId','auditorName','auditNation','startDate','endDate']
	});
	this.store.setDefaultSort('planId', 'desc');	
	this.renderType = function(value){
		return OMS.PLANTYPE[value];
	};
	this.renderExport=function(value){
		var url='../auditplan/exportPlan.form?planId='+value+'&timesnap=' + Date.parse(new Date());
		return '<a href="'+url+'" >导出</a>';
	}
	this.sm =  new Ext.grid.CheckboxSelectionModel();
	this.column = [
		  this.sm,
		  {header: '计划编号', sortable: true, align: 'left',hidden:true, dataIndex: 'planId'},
		  {header: '审核人ID', sortable: true, align: 'left',hidden:true, dataIndex: 'auditorId'},
		  {header: '审核人名称', sortable: true, align: 'left',hidden:true, dataIndex: 'auditorName'},
		  {header: '审核意见', sortable: true, align: 'center',hidden:true, dataIndex: 'auditNation'},
		  {header: '计划名称', sortable: true, align: 'left', dataIndex: 'planName'},
	      {header: '计划类型', sortable: true, align: 'center', width: 20, dataIndex: 'planType',renderer:this.renderType},	      
	      {header: '开始时间', sortable: true, align: 'center', width: 20, dataIndex: 'startDate'},
	      {header: '结束时间', sortable: true, align: 'center', width: 20, dataIndex: 'endDate'},
	      {header: '创建人ID', sortable: true, align: 'left',hidden:true, dataIndex: 'createId'},
	      {header: '部门ID', sortable: true, align: 'left',hidden:true, dataIndex: 'deptId'},
	      {header: '部门名称', sortable: true, align: 'left',hidden:true, dataIndex: 'deptName'},
	      {header: '制定人', sortable: true, align: 'left',width: 40, dataIndex: 'createName'},
	      {header: '导出',  align: 'center',width: 40, dataIndex: 'planId',renderer:this.renderExport}
	];
	
	this.btnReportPlan = new Ext.Button({
		text: '上报计划',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onReportPlan
	});
	this.tbar=[this.btnReportPlan,'->','计划开始时间:', this.dtStart, '~', this.dtEnd,'制定人',this.cbUser,this.btnSearch];
	this.bbar=new Ext.PagingToolbar({
		 pageSize: 15,
         store: this.store,
         displayInfo: true,
         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
         emptyMsg: "没有记录..."
	});
	AuditPlan.Audited.superclass.constructor.call(this, {
		title: '已审核计划',
        region: 'center',
        cm: new Ext.grid.ColumnModel(this.column),
        sm: this.sm,
        tbar:this.tbar,
        bbar:this.bbar,
        store:this.store,
        viewConfig: { forceFit: true },
        listeners:{
        	rowdblclick : this.onViewPlan,
			//render:this.queryPlan
			activate: this.queryPlan
		}
    });
};
Ext.extend(AuditPlan.Audited, Ext.grid.GridPanel, {
	onReportPlan: function(btn, event) {
		if (this.winReport == null) {
			this.winReport = new AuditPlan.WeeklyReport(this);
		}
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn('请选择一条计划！');
			return ;
		}
		var ids='';
		for(var i=0;i<records.length;i++){
			ids+=records[i].data['planId']+',';
		}
		this.winReport.show();	
		this.winReport.initWin(records[0].data,ids);		
	},
	onViewPlan:function(grid, rowIndex,e){		
		if (this.winViewPlan == null) {
			this.winViewPlan = new MyPlan.WeeklyView(this);
		}
		this.winViewPlan.show();
		this.winViewPlan.initWin(grid.store.getAt(rowIndex).data);		
	},
	queryPlan:function(btn,event){
		var param={
			start : 0,
			limit : 15,
			startDateFm:this.dtStart.getValue(),
			startDateTo:this.dtEnd.getValue(),
			createId:this.cbUser.getValue()
		};
		Utils.formatDate(param,'startDateFm');
		Utils.formatDate(param,'startDateTo');
		this.store.load({
			params:param
		});
	}
});
